// pages/college-details/details.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    id:'',
    details:'',
    comment:'',
    inputShowed:false,
    value:'',
  },
  // 获取详情内容
  getdetails:function(){
    ajax('/api/index/college_show', {
      data: {
        id:this.data.id,
        uqid: wx.getStorageSync('uqid')
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      var arr = res.data
      arr.plan_arrs=[]
      var li1 = '.jpg', li2 = '.jpeg', li3 = '.png', li4 = '.gif';
      for (var c in arr.plan_arr) {
        if (arr.plan_arr[c].includes(li1) || arr.plan_arr[c].includes(li2) || arr.plan_arr[c].includes(li3) || arr.plan_arr[c].includes(li4)) {
          arr.plan_arrs[c] = { 'url': arr.plan_arr[c], 'style': '0' }
        } else {
          arr.plan_arrs[c] = { 'url': arr.plan_arr[c], 'style': '1' }
        }
      }

      res.data.desc = res.data.desc.replace(/<img/g, '<img width=100%');
      this.setData({
        details:res.data
      })
    })
  },
  // 获取评论
  getpinglun:function(){
    ajax('/api/index/forum_show_comment', {
      data: {
        id: this.data.id,
        type:1,
        page:1,
        limit:20
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        comment: res.data
      })
    })
  },
  // 获取input  值
  value:function(e){
    this.setData({
      value: e.detail.value,
      inputShowed:true
    })
  },
  // 发表
  fabiao:function(){
    ajax('/api/user/comment_first_add', {
      data: {
        uqid: wx.getStorageSync('uqid'),
        type: 1,
        zid: 1,
        content:this.data.value
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "POST",
    }, res => {
      this.getpinglun()
      this.setData({
        value: '',
        inputShowed: false
      })
    })
  },
  // 点赞
  dianzan:function(){
    ajax('/api/user/love_add_or_del', {
      data: {
        uqid: wx.getStorageSync('uqid'),
        type: 1,
        zid: this.data.select
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "put",
    }, res => {
      this.getdetails()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id : options.id 
    })
    this.getdetails()
    this.getpinglun()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})